package com.molyfun.parents.activity.record;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Base64;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.molyfun.parents.MFPApplication;
import com.molyfun.parents.R;
import com.molyfun.parents.activity.common.BaseActivity;
import com.molyfun.parents.rest.bean.SinaShortUrlBean;
import com.molyfun.parents.utils.PreferencesStorageUtil;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by william(liuchao.hit@gmail.com).
 * Date:11/2/16.
 * 家庭作业详情页接口
 */
public class RecordLocalUploadActivity extends BaseActivity implements View.OnClickListener{

    private static final String FLAG_ANNOUNCEMENT_BEAN = "announcement_bean";

    @InjectView(R.id.base_title_left)
    ImageView mBackBtn;
    @InjectView(R.id.base_title)
    TextView mTitle;
    @InjectView(R.id.url)
    TextView mUrl;

    public static void startActivity(Context content){
        Intent intent = new Intent();
        intent.setClass(content, RecordLocalUploadActivity.class);
        content.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_record_local_upload);

        ButterKnife.inject(this);
        getSupportActionBar().setTitle("");

        mTitle.setText("本地上传");
        mBackBtn.setOnClickListener(this);

        String longUrl = "";
        System.out.println("base64--->" + Base64.encodeToString(PreferencesStorageUtil.getToken(RecordLocalUploadActivity.this).getBytes(), Base64.DEFAULT));

        switch (PreferencesStorageUtil.getServerMode(MFPApplication.getInstance())) {
            case PreferencesStorageUtil.ServerMode.OFFICAL:
                longUrl = "http://nativeweb.elimautism.com/#/upload/" + Base64.encodeToString(PreferencesStorageUtil.getToken(RecordLocalUploadActivity.this).getBytes(), Base64.DEFAULT);
                break;
            case PreferencesStorageUtil.ServerMode.PREPARE:
            case PreferencesStorageUtil.ServerMode.TEST:
                longUrl = "http://nativewebtest.elimautism.com/#/upload/" + Base64.encodeToString(PreferencesStorageUtil.getToken(RecordLocalUploadActivity.this).getBytes(), Base64.DEFAULT);
                break;
        }


        Call<List<SinaShortUrlBean>> call = MFPApplication.getInstance().getRestClient().getShortUrl(longUrl);
        call.enqueue(new Callback<List<SinaShortUrlBean>>() {
            @Override
            public void onResponse(Call<List<SinaShortUrlBean>> call, Response<List<SinaShortUrlBean>> response) {
                if (response.body() != null && response.body().size() > 0) {
                    SinaShortUrlBean sinaShortUrlBean = response.body().get(0);
                    mUrl.setText(sinaShortUrlBean.getUrl_short());
                }
            }

            @Override
            public void onFailure(Call<List<SinaShortUrlBean>> call, Throwable t) {

            }
        });

    }



    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.base_title_left:
                finish();
                break;
        }
    }

}
